{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_cloud_services/blob/main/examples/parse/demo_starter_parse_selected_pages.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Parse Selected Pages \n",
    "\n",
    "In this notebook we will demonstrate how to parse selected pages in a document using LlamaParse.\n",
    "\n",
    "Status:\n",
    "| Last Executed | Version | State      |\n",
    "|---------------|---------|------------|\n",
    "| Aug-19-2025   | 0.6.61  | Maintained |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Installation\n",
    "\n",
    "Here we install `llama-cloud-services` and use `LlamaParse` for parsing the document."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install llama-cloud-services"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Set API Key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "# API access to llama-cloud\n",
    "os.environ[\"LLAMA_CLOUD_API_KEY\"] = \"llx-...\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Download Data\n",
    "\n",
    "Here we download Uber 2021 10K SEC filings data for the demonstration."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/10k/uber_2021.pdf' -O './uber_2021.pdf'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Parse the PDF file in selected pages\n",
    "\n",
    "Here we will parse the PDF file in selected pages and get the text in `markdown` format."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Started parsing the file under job_id d9d7ecc9-766c-48c6-92a8-17432d34818a\n"
     ]
    }
   ],
   "source": [
    "from llama_cloud_services import LlamaParse\n",
    "\n",
    "parser = LlamaParse(\n",
    "    # target pages allows for a few formats: 1,2,3 or 1-3 or 1,3,5-7, etc.\n",
    "    target_pages=\"0,1,2\",\n",
    "    parse_mode=\"parse_page_with_agent\",\n",
    "    model=\"openai-gpt-4-1-mini\",\n",
    "    high_res_ocr=True,\n",
    "    adaptive_long_table=True,\n",
    "    outlined_table_extraction=True,\n",
    "    output_tables_as_HTML=True,\n",
    ")\n",
    "\n",
    "results = await parser.aparse(\"./uber_2021.pdf\")\n",
    "documents = results.get_markdown_documents(split_by_page=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n# UNITED STATES  \\n## SECURITIES AND EXCHANGE COMMISSION  \\nWashington, D.C. 20549  \\n____________________________________________  \\n# FORM 10-K  \\n____________________________________________  \\n\\n(Mark One)  \\n\\n[x] **ANNUAL REPORT PURSUANT TO SECTION 13 OR 15(d) OF THE SECURITIES EXCHANGE ACT OF 1934**  \\nFor the fiscal year ended December 31, 2021  \\nOR  \\n[ ] **TRANSITION REPORT PURSUANT TO SECTION 13 OR 15(d) OF THE SECURITIES EXCHANGE ACT OF 1934**  \\nFor the transition period from_____ to _____  \\nCommission File Number: 001-38902  \\n____________________________________________  \\n\\n# UBER TECHNOLOGIES, INC.  \\n\\n(Exact name of registrant as specified in its charter)  \\n____________________________________________  \\n\\nDelaware | 45-2647441  \\n(State or other jurisdiction of incorporation or organization) | (I.R.S. Employer Identification No.)  \\n\\n1515 3rd Street  \\nSan Francisco, California 94158  \\n(Address of principal executive offices, including zip code)  \\n\\n(415) 612-8582  \\n(Registrant’s telephone number, including area code)  \\n____________________________________________  \\n\\nSecurities registered pursuant to Section 12(b) of the Act:  \\n\\n<table>\\n<thead>\\n<tr>\\n<th>Title of each class</th>\\n<th>Trading Symbol(s)</th>\\n<th>Name of each exchange on which registered</th>\\n</tr>\\n</thead>\\n<tbody>\\n<tr>\\n<td>Common Stock, par value $0.00001 per share</td>\\n<td>UBER</td>\\n<td>New York Stock Exchange</td>\\n</tr>\\n</tbody>\\n</table>\\n\\nSecurities registered pursuant to Section 12(g) of the Act: None  \\n\\n* Indicate by check mark whether the registrant is a well-known seasoned issuer, as defined in Rule 405 of the Securities Act.  \\n  - Yes [x]  \\n  - No [ ]  \\n\\n* Indicate by check mark whether the registrant is not required to file reports pursuant to Section 13 or Section 15(d) of the Act.  \\n  - Yes [ ]  \\n  - No [x]  \\n\\n* Indicate by check mark whether the registrant (1) has filed all reports required to be filed by Section 13 or 15(d) of the Securities Exchange Act of 1934 during the preceding 12 months '"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "documents[0].text[:2000]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(documents)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
